package com.blog.demo.controller;

import com.blog.demo.entity.User;
import com.blog.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author : JiangLinHan
 * Date : 2019/6/14
 * Time : 14:06
 **/
@RestController
@Api(description = "用户操作api")
public class UserController {

    @Autowired
    private UserService userService;

    // test
    @RequestMapping(value = "hello",produces = "application/json; charset=UTF-8")
    public Map<String,Object> hello(){
        Map<String, Object> map = new HashMap<>();
        map.put("data", "hello world!");
        map.put("size", 1);
        return map;
    }

    // 查询所有user
    @ApiOperation(value = "查询所有用户信息", notes = "查询所有用户信息")
    @ApiImplicitParam(paramType = "query", required = true)
    @RequestMapping(value = "findAllUser")
    @ResponseBody
    public ModelAndView findAllUser(){
        ModelAndView model = new ModelAndView();
        List<User> all = userService.findAll();
        model.addObject("list",all);
        model.addObject("length",all.size());
        model.setViewName("list");
        return model;
    }

    // 插入user
    @RequestMapping(value = "saveUser",produces = "application/json; charset=UTF-8")
    public Map<String,Object> saveUser(User user){
        Map<String, Object> map = new HashMap<>();
        try {
            userService.save(user);
            map.put("status","ok");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","error");
        }
        return map;
    }

    // 删除user
    @RequestMapping(value = "deleteUser",produces = "application/json; charset=UTF-8")
    public Map<String,Object> deleteUser(long id){
        Map<String, Object> map = new HashMap<>();
        try {
            userService.deleteAllById(id);
            map.put("status","ok");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","error");
        }
        return map;
    }

    // 根据id查询user
    @ApiOperation(value = "根据id查询用户信息", notes = "根据id查询用户信息")
    @RequestMapping(value = "findUserById",produces = "application/json; charset=UTF-8",method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "用户id", paramType = "query", required = true, dataType = "long")
    public Map<String,Object> findUserById(long id){
        Map<String, Object> map = new HashMap<>();
        User allById = new User();
        try {
            allById = userService.findAllById(id);
            map.put("status","ok");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","error");
        }
        map.put("list",allById);
        return map;
    }

    // 修改
    @RequestMapping(value = "updateUser",produces = "application/json; charset=UTF-8")
    public Map<String,Object> updateUser(User user){
        Map<String, Object> map = new HashMap<>();
        try {
            userService.update(user);
            map.put("status","ok");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","error");
        }
        return map;
    }

}
